package com.helen.ui.newui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.coorchice.library.SuperTextView;
import com.google.gson.Gson;
import com.helen.db.UserHelper;
import com.helen.entity.WalletEntity;
import com.helen.global.Constants;
import com.helen.shopping.R;
import com.helen.ui.BaseActivity;
import com.helen.utils.JsonUtil;
import com.helen.utils.MyLog;
import com.helen.utils.tiputils.MToast;
import com.zhouyou.http.EasyHttp;
import com.zhouyou.http.callback.SimpleCallBack;
import com.zhouyou.http.exception.ApiException;
import com.zhouyou.http.model.HttpParams;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * Created by yang on 2019/1/31
 */
public class WalletActivity extends BaseActivity {
    @BindView(R.id.img_back)
    ImageView imgBack;
    @BindView(R.id.tv_lingqian_detail)
    SuperTextView tvLingqianDetail;
    @BindView(R.id.tv_lingqin)
    TextView tvLingqin;
    @BindView(R.id.tv_time)
    TextView tvTime;
    @BindView(R.id.tv_dismiss_time)
    SuperTextView tvDismissTime;
    @BindView(R.id.tv_ti_xian)
    SuperTextView tvTiXian;
    @BindView(R.id.tv_leiji_money)
    TextView tvLeijiMoney;
    @BindView(R.id.tv_zuori_money)
    TextView tvZuoriMoney;
    @BindView(R.id.tv_leiji_ti_xianmoney)
    TextView tvLeijiTiXianmoney;
    @BindView(R.id.tv_leiji_dismiss_money)
    TextView tvLeijiDismissMoney;

    @Override
    protected int attachLayoutRes() {
        return R.layout.activity_wallet_in;
    }

    @Override
    protected void initInjector() {

    }

    @Override
    protected void initViews() {
        getData();
    }

    @Override
    protected void updateViews() {

    }



    private void getData(){
        mMProgressDialog.show();
        HttpParams params = new HttpParams();
        params.put("id",UserHelper.getInstance().getloginEntity(WalletActivity.this).getId());
        EasyHttp.post(Constants.API_JSONAPI_DIBS)
                .params(params)
                .execute(new SimpleCallBack<String>() {
                    @Override
                    public void onCompleted() {
                        super.onCompleted();
                        mMProgressDialog.dismiss();
                    }

                    @Override
                    public void onError(ApiException e) {
                        MyLog.e("yang", "财富钱包失败==" + e.toString());
                        MToast.makeTextLong(WalletActivity.this, getString(R.string.service_error)).show();
                    }

                    @Override
                    public void onSuccess(String s) {
                        mMProgressDialog.dismiss();
                        MyLog.d("yang","s=="+s);
                        if(!TextUtils.isEmpty(s)){
                            WalletEntity walletEntity = new Gson().fromJson(s,WalletEntity.class);
                            tvLingqin.setText(walletEntity.getOkMoney()+"");
                            tvLeijiMoney.setText(walletEntity.getSumMoney()+"");
                            tvZuoriMoney.setText(walletEntity.getYesterdayMoney()+"");
                            tvLeijiTiXianmoney.setText(walletEntity.getTixianMoney()+"");
                            tvLeijiDismissMoney.setText(walletEntity.getXiaoshiMoney()+"");
                            tvTime.setText(walletEntity.getHour()+"小时");
//                            tvTime.setCountdownTime(walletEntity.getHour()*3600,Math.random()+"");
                        }
                    }
                });
    }
    @OnClick({R.id.img_back, R.id.tv_lingqian_detail, R.id.tv_dismiss_time, R.id.tv_ti_xian})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.img_back:
                finish();
                break;
            case R.id.tv_lingqian_detail:
                startActivity(new Intent(WalletActivity.this,LingQianDetailActivity.class));
                break;
            case R.id.tv_dismiss_time:
                getData();
                break;
            case R.id.tv_ti_xian:
                takeDibsApi();
                break;
        }
    }

      private void takeDibsApi(){
          mMProgressDialog.show();
          HttpParams params = new HttpParams();
          params.put("id",UserHelper.getInstance().getloginEntity(WalletActivity.this).getId());
          EasyHttp.post(Constants.API_JSONAPI_TAKE_DIBS)
                  .params(params)
                  .execute(new SimpleCallBack<String>() {
                      @Override
                      public void onCompleted() {
                          super.onCompleted();
                          mMProgressDialog.dismiss();
                      }

                      @Override
                      public void onError(ApiException e) {
                          MyLog.e("yang", "提现失败==" + e.toString());
                          MToast.makeTextLong(WalletActivity.this, getString(R.string.service_error)).show();
                      }

                      @Override
                      public void onSuccess(String s) {
                          mMProgressDialog.dismiss();
                          MyLog.d("yang","s=="+s);
                          int code = JsonUtil.getJsonValuesInt(s,"code");
                          String message = JsonUtil.getJsonValuesString(s,"msg");
                          if(code==0){
                              getData();
                              MToast.makeTextLong(WalletActivity.this, "提现成功").show();
                          }else {
                              MToast.makeTextLong(WalletActivity.this, message).show();
                          }

                      }
                  });
      }

}
